package comparator;

import java.util.Comparator;
import java.util.List;

import controller.Controller;

import dataBase.Business;
import dataBase.Order;
import dataBase.Status;

public class SortByStatus {


	private Business business;
	
	private static Comparator<Order> com;
	
	public SortByStatus() {
	com = new Comparator<Order>() {
		@Override
		public int compare(Order o1, Order o2) {
			List<Status> allStatuses = Controller.getInstance().getStatusesOfBiz(business.getUserName());
			int location=1;
			int firstLocation=-1;
			int secondLocation=-1;
			//get current status of orders, in the last location of order's level list
			String firstStatusName = Controller.getInstance().getCurrentStatusOfOrder(o1);
			String secondStatusName = Controller.getInstance().getCurrentStatusOfOrder(o2);
			System.out.println("11111 - > "+firstStatusName+" 22222 - > "+secondStatusName);
			if (allStatuses==null)
				System.out.println("*****%%%%%%%%****");
			for (Status status:allStatuses){
				//if the status is the same as current status, change the relative location
				if (status.getStatusName().equals(firstStatusName))
					firstLocation=location;
				if (status.getStatusName().equals(secondStatusName))
						secondLocation=location;
				//if both statuses were changed, then break
				if (firstLocation!=-1 && secondLocation!=-1)
					break;
				//else, location is increasing by 1
				location++;
			}
			//return the difference between the statuses
			return (firstLocation-secondLocation);
		}
		};//end of comparator
	}
	public void setBusiness(Business business){
		this.business=business;
		System.out.println("set to biz");
	}
	public Comparator<Order> getStatusComparator(){
		return com;
	}
}
